const db = require('../common/Db');
const game_config = require('../config/game_config');
const log = require('../utils/log');
const util = require('util')

class hallDb extends db{
    static getInstance(){
        if(!hallDb.instance){
            hallDb.instance = new hallDb();
            return hallDb.instance;
        }else{
            return hallDb.instance;
        }
    }

    constructor(){
        super();
        log.info('======Db 链接数据库=========')

        const config = {
            host: game_config.center_database.host,
            port: game_config.center_database.port,
            user: game_config.center_database.uname,
            password: game_config.center_database.upwd,
            database: game_config.center_database.db_name
        };
        this.myDb = new db(config);
        // this.test();
        //
        // try {
        //     await this.myDb.connect();
        //     const sql = 'select * from userinfo where userId = "web_ti6lrZGZ"';
        //     const rows = await this.myDb.query(sql, [1]);
        //     console.log("======tesr========",rows);
        // } finally {
        //     await this.myDb.close();
        // }
    }

    //插入一条数据
    insetUserToMsql(uid, name, avator,isNewPlayer){
        var sql = "insert into userinfo(`userId`, `name`, `avator`,`isNewPlayer`)values(\"%s\", \"%s\", \"%s\", \"%d\")";
        var sql_cmd = util.format(sql, uid, name, avator,isNewPlayer);
        log.info('========getUserInfo===========',sql_cmd)
        this.myQuery(sql_cmd,()=>{},()=>{});
    }

    //设置玩家新状态  是否是新用户 1 是， 2 不是
    updateUserIsNew(uid, isNewPlayer){
        var sql = 'update userinfo set isNewPlayer = \"%d\" where userId = \"%s\"';
        var sql_cmd = util.format(sql, isNewPlayer, uid);
        this.myQuery(sql_cmd,()=>{},()=>{});
    }

    //更新用户的奖励的球币和抽奖券
    async update_user_rewards(uid,qiuNum,quanNum, isNewPlayer){
        var sql = 'update userinfo set ballNum = \"%d\", ticket = \"%d\" ,isNewPlayer = \"%d\" where userId = \"%s\"';
        var sql_cmd = util.format(sql, qiuNum,quanNum, isNewPlayer, uid);
        try {
            await this.myDb.connect();
            const rows = await this.myDb.query(sql_cmd, [1]);
            console.log("======update_user_rewards========",rows);
            return rows;
        } finally {
            await this.myDb.close();
        }
    }

    //查询tm_db 表里面的数据
    async get_tm_db_info_mysql(uid, type, callback = null){
        log.info('========get_tm_db_info_mysql=======',uid,type)
        let key = `key__${uid}_${type}`;
        var sql = 'select * from tm_db where keyOnly =\"%s\"';
        var sql_cmd = util.format(sql, key);
        try {
            await this.myDb.connect();
            const rows = await this.myDb.query(sql_cmd, [1]);
            console.log("======get_tm_db_info_mysql11========",rows);
            return rows;
        } finally {
            await this.myDb.close();
        }
    }

    async get_tm_db_info_mysql_by_uid(uid,callback = null){
        log.info('========get_tm_db_info_mysql=======',uid)
        var sql = 'select * from tm_db where uid =\"%s\"';
        var sql_cmd = util.format(sql, uid);
        try {
            await this.myDb.connect();
            const rows = await this.myDb.query(sql_cmd, [1]);
            console.log("======get_tm_db_info_mysql_by_uid========",rows);
            return rows;
        } finally {
            await this.myDb.close();
        }
    }

    //按段位等级查询tm_db 的前3名
    async get_tm_db_by_lv_rank3_mysql(type){
        var sql = 'select * from tm_db where gamType = \"%d\" order by dwLv desc limit 3';
        var sql_cmd = util.format(sql,type);
        try {
            await this.myDb.connect();
            const rows = await this.myDb.query(sql_cmd, [1]);
            console.log("======get_tm_db_info_mysql_by_uid========",rows);
            return rows;
        } finally {
            await this.myDb.close();
        }
    }

    //按段勋章数量查询tm_db 的前3名
    async get_tm_db_by_xzNum_rank3_mysql(type){

        var sql = 'select * from tm_db where gamType = \"%d\" order by dwXzNum desc limit 3';
        var sql_cmd = util.format(sql,type);
        try {
            await this.myDb.connect();
            const rows = await this.myDb.query(sql_cmd, [1]);
            console.log("======get_tm_db_info_mysql_by_uid========",rows);
            return rows;
        } finally {
            await this.myDb.close();
        }
    }

    // 修改tm_db 房间号
    async update_tm_db_room_id(uid,type, roomdId){

        let key = `key__${uid}_${type}`;
        var sql = 'update tm_db set roomId = \"%d\" where keyOnly = \"%s\"';
        var sql_cmd = util.format(sql,roomdId, key);
        try {
            await this.myDb.connect();
            const rows = await this.myDb.query(sql_cmd, [1]);
            console.log("======get_tm_db_info_mysql_by_uid========",rows);
            return rows;
        } finally {
            await this.myDb.close();
        }
    }
}
global.hallSerMgr.hallDb = hallDb.getInstance();
module.exports = hallDb;